package servlet.zj;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DAOFactory;
import dao.zj.BrandDao;
import dao.zj.ProductDao;
import entity.Brand;
import entity.Product;

/**
 * Servlet implementation class ProductAll
 */
@WebServlet("/ProductAll")
public class ProductAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProductDao one=DAOFactory.createDAO();
	private BrandDao two=DAOFactory.createBrandDAO();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int curpage = Integer.parseInt(
				request.getParameter("curpage"));
		int pagesize = Integer.parseInt(
				request.getParameter("pagesize"));
		
		String brand="";
		String state="";
		String text="";
		if(!(request.getParameter("brand")==null)){
			brand=request.getParameter("brand");
			request.getSession().setAttribute("brand",null);
		}
		if(!(request.getParameter("state")==null)){
			state=request.getParameter("state");
			request.getSession().setAttribute("state",null);
		}
		if(!(request.getParameter("text")==null)){
			text=request.getParameter("text");
			request.getSession().setAttribute("text",null);
		}
		if(request.getSession().getAttribute("brand")==null){
			request.getSession().setAttribute("brand",brand);
		}
		if(request.getSession().getAttribute("state")==null){
			request.getSession().setAttribute("state",state);
		}
		if(request.getSession().getAttribute("text")==null){
			request.getSession().setAttribute("text",text);
		}
		
		brand=request.getSession().getAttribute("brand").toString();
		state=request.getSession().getAttribute("state").toString();
		text=request.getSession().getAttribute("text").toString();

		
		int total=one.countAll(brand, state, text);
		request.setAttribute("total", total);
		List<Product> list=one.selectAll(brand, state, text, curpage, pagesize);
		request.setAttribute("list", list);
		List<Brand> a=two.select();
		request.setAttribute("brand", a);
		request.getRequestDispatcher("product.jsp").forward(request, response);
	}
}
